package com.dao.imp;

import com.entity.Order;
import com.entity.ShoppingCart;
import com.util.DBConn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Mathison
 * @Title: FindOrder
 * @Package: com.dao.imp
 * @Description: TODO
 * @date 2021/11/28 19:32
 **/
public class FindOrder {
    public static Order getOrderInfo(String sql){
        Order order = new Order();
        try{
            DBConn.init();
            ResultSet resultSet = DBConn.selectSql(sql);
            while (resultSet.next()){
                order.setGood_amount(JSONObject.fromObject(resultSet.getObject("good_amount")));
//                System.out.println(resultSet.getInt("order_id"));
                order.setOrder_id(resultSet.getInt("order_id"));
                order.setUser_id(resultSet.getInt("user_id"));
                order.setGood(JSONObject.fromObject(resultSet.getObject("good")));
                order.setReceiver_address(JSONObject.fromObject(resultSet.getObject("receive_address")));
                order.setOrder_createTime(resultSet.getString("order_creatTime"));
//                System.out.println(resultSet.getDate("order_creatTime"));
                order.setStatus(resultSet.getBoolean("status"));
            }
//            DBConn.closeConn();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return order;
    }

    public static List<Order> getOrderList(String sql){
        List<Order> orderList = new ArrayList<>();

        try{
            DBConn.init();
            ResultSet resultSet = DBConn.selectSql(sql);
            while (resultSet.next()){
                Order order = new Order();
                order.setGood_amount(JSONObject.fromObject(resultSet.getObject("good_amount")));
//                System.out.println(resultSet.getInt("order_id"));
                order.setOrder_id(resultSet.getInt("order_id"));
                order.setUser_id(resultSet.getInt("user_id"));
                order.setGood(JSONObject.fromObject(resultSet.getObject("good")));
                order.setReceiver_address(JSONObject.fromObject(resultSet.getObject("receive_address")));
                order.setOrder_createTime(resultSet.getString("order_creatTime"));
//                System.out.println(resultSet.getDate("order_creatTime"));
                order.setStatus(resultSet.getBoolean("status"));
                order.setPay(resultSet.getBoolean("isPay"));
                orderList.add(order);
            }
//            DBConn.closeConn();
        }catch (SQLException e){
            e.printStackTrace();
        }

        return orderList;
    }

    public  static int userGetTotalOrderNumber(int user_id){
        String sql = "SELECT COUNT(*) FROM orders WHERE user_id="+user_id;
        int count = 0;
        DBConn.init();
        count = DBConn.countSql(sql);
        return count;
    }
    public static int getOrderNumber(){
        String sql = "SELECT COUNT(*) FROM orders WHERE isPay=1";
        int count = 0;
        DBConn.init();
        count = DBConn.countSql(sql);
        return count;
    }
}
